package servlet;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MoveAction;

import domain.Movie;
import ex.DatabaseException;
import ex.ProjectCompositionFailedException;

import service.JobService;
import service.MovieService;


public class YellowPages extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		domain.User user=null;
		HttpSession session = request.getSession();
		MovieService movieService = new MovieService();
		JobService jService = new JobService();
		LinkedList<Movie> movies = new LinkedList<Movie>();
		if (session.getAttribute("user") != null) {
			user = (domain.User) session
					.getAttribute("user");
			if(user == null){
				response.sendRedirect("home");
			}else{
				try {
					movies = movieService.getAllMovies();
					Iterator<Movie> itr = movies.iterator();
					while(itr.hasNext()){
						Movie mv = itr.next();
						mv.setActorJobs(jService.getActorJobsByMovie(mv));
						mv.setCrewJobs(jService.getCrewJobsByMovie(mv));
						System.out.println(mv.getName());
					}
				} catch (DatabaseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ProjectCompositionFailedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				request.setAttribute("movies", movies);
				RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/user/yellow-pages.jsp");
				if (dispatcher != null) dispatcher.forward(request, response);
		}
	}else{
		response.sendRedirect("home");
	}
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub
}

}
